(defpackage :tutorial 
    (:use :common-lisp :excl :net.aserve :net.html.generator :gen-res :db.agraph.user))

(in-package :tutorial)

(publish-file :path "/rpn" :file "d:\\r\\rpn.html")

(defun rrequest (name req)
  (cdr (assoc name (request-query req) :test #'equal))
  )

(defun text-request (name req)
  (let ( 
        ( r (rrequest name req))
        )
    (if r r " ")
    ))

(defun int-request (name req)
  (let* (
         ( r (rrequest name req))
         ( x (parse-integer r :junk-allowed t))
         ( xx (if x x 0))
         )
    xx
    ))

(defmacro test-html (&rest body)
  `(with-output-to-string  (*html-stream*) ,@body)
  )


                
(defparameter *cholesterol* 
  `(gaya 204  ldl 119 hdl 52 trig 129
         phil 220 ldl 140 hdl 44 trig 97))


(publish :path "/hello2"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
            (with-http-body (req ent)
               (html 
                  (:html (:head (:title "Gayatri Shony Pony Kiss Kiss"))
                         (:body 
                           ((:font :color "red") "Gayatri Shony Pony  ")
                          ((:font :color "blue") "Kiss Kiss!"))))))))


(defun get-all-triples-for-me ()
  
  (db.agraph.user::s0d (?s ?o ?p) (db.agraph.user::q- ?s ?o ?p))
  
  )

(defun get-all-milestones-for-me ()
  
  (db.agraph.user::show-ms)
  
  )

(defun get-all-actors-for-me ()
  
  (db.agraph.user::show-actors)
  
  )

(defmacro something (l)
  `(mapcar #'(lambda (it) (html (:princ-safe it))) ,l))
  
  
;;;  
;;;  (when l
;;;    `(progn 
;;;       (html (:princ-safe (car ,l)))
;;;       (something (cdr ,l))
;;;      )))
;;;    
 



(publish :path "/enter-new-actor"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( 
                  ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ; ( the-creation-date (text-request "creation-date" req))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                   

                   )
               (html 
                  (:html (:head (:title "Actors"))
                         (:body 
                          (:ul (:li "same as foaf:actor")
                               (:li "actor can be a singe human or a group or any other entity"  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          ((:form :action "create-new-actor")
                           (:table
                            (nice-text-input "name" the-name)
                           (nice-text-input "description" the-description)
                            ; (nice-text-input "creation-date" the-creation-date)
                            (nice-text-input "comment" the-comment)
                            (nice-text-input "link" the-link)
                             
                            (submit-button)
                            ) ; end table
                           ) ; end form
                         ); end body
                         )) ;end html
               ) ; end let
             ))))




(defun commit-actor ( the-id the-name the-description the-creation-date the-comment the-link )
  (let (
        ( the-resource-as-list (list 'res 'actor
                                     'id the-id
                                     'name the-name
                                     'description the-description
                                     'creation-date the-creation-date
                                     'comment the-comment
                                     'link the-link
                                     )))
    
    (gen-res::commit-resource-as-list the-resource-as-list)
    ))


(defun get-global-id (the-list)
  (let* ((s (with-output-to-string (out) (mapcar #'(lambda (it) (format out "~a" it)) the-list))))
  s))


(publish :path "/create-new-actor"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( 
                   ; (the-triples (cons '("ID" "Name" ) (get-all-actors-for-me) ))
                   ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ( the-creation-date (text-request "creation-date" req))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                    
                   )
                ; ( commit-actor the-id the-name the-description the-creation-date the-comment the-link )

               (html 
                  (:html (:head (:title "Actors"))
                         (:body 
                          (:ul (:li "Here is the new actor that you created")
                              (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          (make-table `( ("Parameter" "Value")
                                        ("name" ,the-name)
                                        ("description" ,the-description)
                                        ("creation-date" ,the-creation-date)
                                        ("comment" ,the-comment)
                                        ("link" ,the-link))
                                        
                            ) ; end table
                         ); end body
                         )) ;end html
               ) ; end let
             ))))





(publish :path "/list-all-actors"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( (the-triples (cons '("ID" "Name" ) (get-all-actors-for-me) )))
               (html 
                  (:html (:head (:title "Actors"))
                         (:body 
                          (:ul (:li "same as foaf:actor")
                               (:li "actor can be a singe human or a group or any other entity"  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                                    :p :hr :p
                          
                          
                          (make-table the-triples) :p :hr
                          
                         
                         ); end body
                         )) ;end html
               ) ; end let
             ))))


(publish :path "/list-all-milestones"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( (the-triples (cons '("ID" "Name" ) (get-all-milestones-for-me) )))
               (html 
                  (:html (:head (:title "Milestones"))
                         (:body 
                          (:ul (:li "Not to be confused with millstones")
                               (:li "Though they can feel like that sometimes"  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                                    :p :hr :p
                          
                          
                          (make-table the-triples) :p :hr
                          
                         
                         ); end body
                         )) ;end html
               ) ; end let
             ))))


;;;(defmacro all-or-nothing (the-condition the-value)
;;;  (when the-condition the-value))

(defmacro nice-text-input (the-name the-value &optional (max-length "80") )
  `(:tr (:td ,the-name) (:td   ((:input :type "text" 
                                        :name ,the-name 
                                        :value ,the-value 
                                        :maxlength ,max-length 
                                        :size ,max-length
                                        ; ,(all-or-nothing disabled "DISABLED")
                                        )))))




(publish :path "/enter-new-milestone"
    :content-type "text/html"
    :function 
         #'(lambda (req ent)
             (let* (
                    ; ( the-id (text-request "ID" req) )
                    ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ; ( the-creation-date (text-request "creation-date" req))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                    ( the-owner (text-request "owner" req))
                    ( the-target-date (text-request "target-date" req))
                    ( the-predecessor (text-request "predecessor" req))
                    
                      ; ( f (int-request "m-frequency" req) )
                      )

         (with-http-response (req ent)
           (with-http-body (req ent)
             ; (let ((count 0 ))
               (html 
                  (:html (:head (:title "Enter New Milestone"))
                         (:body 
                          (:ul (:li "Not to be confused with millstones")
                               (:li "Though they can feel like that sometimes"  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          ((:form :action "create-new-milestone")
                           (:table
                            ; (:tr (:td "name") (:td   ((:input :type "text" :name "name" :value the-name :maxlength "80"))))
                            ; (nice-text-input "ID" the-id)
                            (nice-text-input "name" the-name)
                            (nice-text-input "description" the-description)
                            ; (nice-text-input "creation-date" the-creation-date)
                            (nice-text-input "comment" the-comment)
                            (nice-text-input "link" the-link)
                            (nice-text-input "owner" the-owner)
                            (nice-text-input "target-date" the-target-date)
                            (nice-text-input "predecessor" the-predecessor)
                            

                            (submit-button)
                            ) ; end table
                           ) ; end form
                         ); end body
                         )) ;end html
               ; ) ; end let
             )))))



(defun get-current-date-in-the-appropriate-format (&optional out )
  (multiple-value-bind (s m h d mo y dow dst tz) (get-decoded-time)
    (declare (ignore s m h dow dst tz))
    (format out "~4,'0d-~2,'0d-~2,'0d" y mo d)))
    


(publish :path "/create-new-milestone"
    :content-type "text/html"
    :function 
         #'(lambda (req ent)
             (let* (
                    ( the-name (text-request "name" req) )
                    ( the-description (text-request "description" req))
                    ( the-creation-date (get-current-date-in-the-appropriate-format))
                    ( the-comment (text-request "comment" req))
                    ( the-link (text-request "link" req))
                    ( the-owner (text-request "owner" req))
                    ( the-target-date (text-request "target-date" req))
                    ( the-predecessor (text-request "predecessor" req))
                    ( the-id (commit-milestone  the-name the-description the-creation-date the-comment the-link the-owner the-target-date the-predecessor) )
                    )
         (with-http-response (req ent)
           (with-http-body (req ent)
             ; (let ((count 0 ))
               (html 
                  (:html (:head (:title "New Milestone"))
                         (:body 
                          (:ul (:li "Here is the new milestone you just created :-)")
                               (:li "..."  )
                               (:li ((:a :href "home") "Home") :br)
                               )
                          :p :hr :p
                          
                          ( make-table (list 
                                        (list "ID" the-id)
                                        (list "name" the-name)
                                        (list "description" the-description)
                                        (list "creation-date" the-creation-date)
                                        (list "comment" the-comment)
                                        (list "link" the-link)
                                        (list "owner" the-owner)
                                        (list "target-date" the-target-date)
                                        (list "predecessor" the-predecessor)
                                        ))))))))))
                          
                          
                          
                          
                          
                          
                          
;;;                          ((:form :action "create-new-milestone")
;;;                           (:table
;;;                            ; (:tr (:td "name") (:td   ((:input :type "text" :name "name" :value the-name :maxlength "80"))))
;;;                            (:tr (:td "ID") (:td   (:princ-safe the-id)))
;;;                            (nice-text-input "name" the-name)
;;;                            (nice-text-input "description" the-description)
;;;                            (:tr (:td "creation-date") (:td   the-creation-date ))
;;;                            (nice-text-input "comment" the-comment)
;;;                            (nice-text-input "link" the-link)
;;;                            (nice-text-input "owner" the-owner)
;;;                            (nice-text-input "target-date" the-target-date)
;;;                            (nice-text-input "predecessor" the-predecessor)
;;;                            
;;;
;;;                            ; (submit-button)
;;;                            ) ; end table
;;;                           ) ; end form
;;;                         ); end body
;;;                         )) ;end html
;;;               ; ) ; end let
;;;             )))))




(defun id-from-list (the-list)
  (let ((x (with-output-to-string (out) 
             (mapcar #'(lambda (it) (princ it out)) the-list)
             )))
    (gen-res::crc-s x)))
        




(defun commit-milestone ( the-name the-description the-creation-date the-comment the-link the-owner the-target-date the-predecessor)
  (let* (
        ( the-resource-as-list (list ; 'res 'mile-stone
                                     ; 'id the-id
                                     'name the-name
                                     'description the-description
                                     'creation-date the-creation-date
                                     'comment the-comment
                                     'link the-link
                                     'owner the-owner
                                     'target-date the-target-date
                                     'predecessor the-predecessor
                                ))
         (the-id (id-from-list the-resource-as-list) )
        )
    
    
    
    (gen-res::commit-resource-as-list-02 'mile-stone the-id the-resource-as-list)
    the-id
    ))



(defun nice-link (rel-path &optional text)
  (if text
      (html ((:a :href rel-path ) text) :br)
    (html ((:a :href rel-path ) rel-path ) :br)
    ))

;;;(defmacro nice-raw-link (rel-path text)
;;;  `((:a :href "list-all-milestones") "list-all-milestones") :br)

    
  


(publish :path "/home"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             ; (let ( ( count 0))
               (html 
                  (:html (:head (:title "Home"))
                         (:body 
                          (:ul (:li "Theres no place like home...")
                               (:li "...and this is no place like home!"  )
                               )
                          :p :hr :p
                          (:ul 
                           (:li ((:a :href "list-all-milestones") "List All Milestones") :br)
                           (:li ((:a :href "list-all-triples") "List All Triples") :br)
                           (:li ((:a :href "list-all-actors") "List All Actors") :br)
                           (:li ((:a :href "enter-new-milestone") "Enter New Milestone") :br)
                           (:li ((:a :href "enter-new-actor") "Enter New Actor") :br)
                           
                               )
                         ); end body
                         )) ;end html
               ;  ) ; end let
             )))) 






(publish :path "/list-all-triples"
    :content-type "text/html"
    :function 
    #'(lambda (req ent)
         (with-http-response (req ent)
           (with-http-body (req ent)
             (let ( (the-triples (cons '("Subject" "Predicate" "Object") (get-all-triples-for-me) )))
               (html 
                  (:html (:head (:title "OMG Here are all the Triples!"))
                         (:body 
                          (:ul (:li "Pretty dry stuff, isn't it.")
                               (:li "Don't miss the pokemon"  )
                               (:li "Not in any particular order because this is by the internal indexing")
                               (:li "There are " (:princ-safe  (db.agraph.user::triple-count)) " triples in this repository")
                               (:li ((:a :href "home") "Home") :br)
                               )
                                    :p :hr :p
                          
                          
                          (make-table the-triples) :p :hr
                          
                         
                         ); end body
                         )) ;end html
               ) ; end let
             ))))



    

(defmacro w-select-option-with-selected (text-value comment)
  `(html ((:option value ,text-value :selected "selected") (:princ-safe ,comment))))

  
(defmacro w-select-option-sans-selected (text-value comment)
  `(html ((:option value ,text-value) (:princ-safe ,comment))))


(defun w-select-option (current-value numeric-value text-value comment)
  (if (equal current-value numeric-value)
      (w-select-option-with-selected text-value comment)
    (w-select-option-sans-selected text-value comment)
    ))
    
      
  


(defun submit-button ()
  (html  :p ((:input :type "submit" :value "submit" ) )  )
  )

(defun w-print-description (n)
  (mapcar #'(lambda (it) (html (:td (:princ-safe it)))) n))

(defun w-print-descriptions (n)
  (mapcar #'(lambda (it) (html (:tr (w-print-description it)))) n))





(defmacro mmake-generic-html-sequence ( internal the-list)
  `(mapcar #'(lambda (it) (html (,internal (:princ-safe it)))) ,the-list))

(defmacro make-generic-html-sequence ( external internal the-list)
  `(html ( ,external (mmake-generic-html-sequence ,internal ,the-list ))))

(defun make-uo-list (x)
  (make-generic-html-sequence :ul :li x))


(defun mmake-unordered-list (x)
  (mapcar #'(lambda (it) (html (:li (:princ-safe it)))) x))

(defun make-unordered-list (x)
  (html ( :ul (mmake-unordered-list x))))
  

(defun make-table-cell (x &key bold)
  (if (listp x) (html :td (make-uo-list x))
    (if bold (html (:td (:b (:princ-safe x))))
    (html (:td (:princ-safe x))))
    ))
      


(defun make-row (the-list &key bold)
  (mapcar #'(lambda (it) (make-table-cell it :bold bold)) the-list))
  ; (mapcar #'(lambda (it) (html (:td (:princ-safe it)))) the-list))

(defun mmake-table (the-llist )
  (mapcar #'(lambda (it) (html :tr (make-row it))) the-llist))

(defun make-table (the-llist )
  (html ((:table :bgcolor "white" :bordercolor "black" :border "1" :cellpadding "1" :cellspacing "1")
         (make-row (car the-llist) :bold t)
        (mmake-table (cdr the-llist)))))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

